(function() {
	var app = angular.module('hostMonitorApp', [ 'ui.bootstrap' ]);
	app.controller('hostMonitorController', function($http, $scope, $log,
			$uibModal,$sce) {
		$scope.serviceInfo = {};
		
		$scope.hostName = "主机选择";
		var jsonResult;
		// 程序初期化
		this.init = function() {
			//getTreeData("tuopu");
//			$scope.url = $sce.trustAsResourceUrl('www.baidu.com');
//			$('#hostmonitorDiv').addClass('height: auto');
//			$('#maincontainer').addClass('overflow: hidden');
//			$('.wrapChart3').css('display: none');
		}
		// 程序初期化
		this.init();
		
		// 选择主机弹窗  
		this.selectHostZTreeHtml = function(data) {
			
			var modalInstanceAdd = $uibModal.open({
						backdrop : 'static',
						keyboard : false,
						scope : $scope,
						templateUrl : 'js/controller/view.display/hostMonitorZTree.html',// 模态框的页面内容,这里的url是可以自己定义的,也就意味着什么都可以写
						controller : 'hostMonitorZTreeController',// 这是模态框的控制器,是用来控制模态框的
						ariaLabelledBy : 'modal-title',
						ariaDescribedBy : 'modal-body',
					});

			modalInstanceAdd.opened.then(function(){//模态窗口打开之后执行的函数  
				//获取树状图数据
				getTreeData("tuopu");
        
			}); 
		}
		
		// 获取树状图数据
		function getTreeData(paramater) {
			var basePath = $scope.getAppBasePath();
			var address = "";
			$.ajax({
				type : "POST",
				url : basePath + "/get/gethostMonitorTreeData",
				dataType : "json",
				contentType : "application/json",
				success : function(data) {
					var resultArrary = JSON.parse(data);

					var setting = {
						// 异步加载POST方法
						async : {
							enable : true,
							contentType : "application/json",
							dataType : "text",
							type : "post",
							url : basePath + "/get/asyncGetHostMonitorTreeData",
							autoParam : [ "commentId" ],
							dataFilter : ajaxDataFilter
						},
						callback : {
//							beforeAsync: zTreeBeforeAsync,
							onClick : zTreeOnClick,
							onExpand: zTreeOnExpand//点击节点前+号加载
						}
					};
					function zTreeBeforeAsync(treeId, treeNode) {
					    return (treeNode.isParent == true);
					};
					// 返回数据转成json
					function ajaxDataFilter(treeId, parentNode, responseData) {
						responseData = JSON.parse(responseData);
						return responseData;
					}
					;
					var zTreeObj = $.fn.zTree.init($("#tuopu"), setting,
							resultArrary);
					$("#search-bt").click(filter);
					//默认点击展开根目录
					$("#tuopu_1_switch").click();
					function zTreeOnExpand(event, treeId, treeNode){
						var nodes = zTreeObj.getSelectedNodes();
						if (nodes.length>0 && treeNode.level != 0 && treeNode.level != 1) {
							zTreeObj.reAsyncChildNodes(nodes[0], "refresh");
							}
					}
					function zTreeOnClick(event, treeId, treeNode) {
						var te = treeNode.sytemParameterId;
						var serchId=document.getElementById("serchConfirm");
						serchId.setAttribute("value",te);
						if(treeNode.isParent == false){
							$scope.serviceInfo.sytemParameterId  = treeNode.sytemParameterId;
							
							//显示是主机名
							$("#selectHostDiv").html(treeNode.name);
							//关闭弹窗
							$("#hosttreecancel").click();
						}
					}
					
					//过滤ztree显示数据
					var hiddenNodes;
					function filter(){
						//显示上次搜索后背隐藏的结点
						zTreeObj.showNodes(hiddenNodes);
						var _keywords=$("#keyword").val();
						if(_keywords == null || _keywords ==""){
							zTreeObj.showNodes(hiddenNodes);
						}else{
							var nodes = zTreeObj.getNodesByParamFuzzy("name", _keywords, null);  //根据节点数据的属性搜索，获取条件模糊匹配的节点数据 JSON 对象集合,(keywords)模糊匹配只能针对 String 类型的数据
							if (nodes.length>0) { 
								zTreeObj.selectNode(nodes[0]); //会让节点自动滚到到可视区域内
							} 

							//查找不符合条件的叶子节点
							function filterFunc(node){
								var _keywords=$("#keyword").val();
								if(node.isParent||node.name.indexOf(_keywords)!=-1){
									return false;
								}else{
									return true;	
								}
							};

							//获取不符合条件的叶子结点
							hiddenNodes = zTreeObj.getNodesByFilter(filterFunc);
							
							//隐藏不符合条件的叶子结点
							zTreeObj.hideNodes(hiddenNodes);
						}
					};
					//弹窗title显示
					$("#modalTitle").html("选择主机");
				}
			});
}
		//开始
		this.showResult=function (){
				$scope.url = '';
				var serchId=document.getElementById("serchConfirm");
				$scope.serviceInfo.sytemParameterId  = serchId.getAttribute("value");
				var basePath = $scope.getAppBasePath();
				$.ajax({
					type : "POST",
					url : basePath
							+ "/get/getGrafanaUrl",
					dataType : "json",
					contentType : "application/json",
					data : JSON
							.stringify($scope.serviceInfo.sytemParameterId),
					success : function(data) {
						$scope.url = $sce.trustAsResourceUrl(data);
						$scope.$apply(); 
					},
				error : function(XMLHttpRequest, textStatus){
					$scope.url = $sce.trustAsResourceUrl('about:blank');
					$scope.$apply(); 
				}
				});
				
			}
				
				
				//结束
//	}).controller('servicePopuController',function($scope, $uibModalInstance, $http, $log){
//		$scope.load = function() {
//			//原系统数据前后台交互
//			var basePath = $scope.getAppBasePath();
//			$scope.serviceInfo.HostSequenceNumber = "";
//			$scope.serviceInfo.ip = "";
//			$scope.serviceInfo.hostName = "";
//			$scope.serviceInfo.useResponsibilityDepartment = "";
//			$scope.serviceInfo.useResponsibility = "";
//			$scope.serviceInfo.maintenanceResponsibleDepartment = "";
//			$scope.serviceInfo.maintenanceResponsiblePerson = "";
//			$.ajax({
//				type : "POST",
//				url : basePath
//						+ "/get/getServiceInfo",
//				dataType : "json",
//				contentType : "application/json",
//				data : JSON
//						.stringify($scope.parmeater),
//				success : function(data) {
//					var jsonResult = angular.fromJson(data);
//					$scope.serviceInfo.HostSequenceNumber = jsonResult.HostSequenceNumber;
//					$scope.serviceInfo.ip = jsonResult.ip;
//					$scope.serviceInfo.hostName = jsonResult.hostName;
//					$scope.serviceInfo.useResponsibilityDepartment = jsonResult.useResponsibilityDepartment;
//					$scope.serviceInfo.useResponsibility = jsonResult.useResponsibility;
//					$scope.serviceInfo.maintenanceResponsibleDepartment = jsonResult.maintenanceResponsibleDepartment;
//					$scope.serviceInfo.maintenanceResponsiblePerson = jsonResult.maintenanceResponsiblePerson;
//					$scope.$apply();  
//				}
//			});
//			
//		};
//		$scope.cancel = function() {
//			$uibModalInstance.dismiss('cancel');
//		};
//		
//		//页面访问量波动排名弹出层
	})
	
	/*以上为原始代码；一下为2018.07.07修改  张寅寅*/
	//ztree弹窗js
	.controller(
			'hostMonitorZTreeController',
			function($scope, $uibModalInstance, $http, $log,$filter) {
				//关闭事件
				$scope.cancel = function() {
					// dismiss也是在模态框关闭的时候进行调用,而它返回的是一个reason
					$uibModalInstance.dismiss('cancel');
				};
			});
	
	
})();